package com.dandan.mobilelover.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.dandan.mobilelover.common.ViewHelper;
import com.dandan.mobilelover.entity.User;
import com.dandan.mobilelover.service.UserService;

@SessionAttributes("currentUser")
@Controller
public class UserController {
	
	@Inject
	private UserService userService;
	
	@RequestMapping(value="/",  method = RequestMethod.GET)
	public String index() {
		return "login";
	}

	@RequestMapping(value="/login",  method = RequestMethod.POST)
	public String login(String passport, String password, ModelMap model){
		System.out.println("passport:" + passport + " password:" + password);
		User user = userService.checkLogin(passport, password);
		if(user ==null){
			ViewHelper.flash(model, "用户名密码错误，请重新输入");
			return "login";
		}
		model.addAttribute("currentUser", user);
		return "redirect:home";
	}
	
	@RequestMapping(value="/home",  method = RequestMethod.GET)
	public String home(){
		return "home";
	}
	
	@RequestMapping(value="register", method= RequestMethod.GET)
	public String register(){
		User user  = new User();
		user.setNickname("叫我雷锋吧");
		user.setEmail("ahuiwanglei@126.com");
		user.setIntegration(10);
		userService.saveUser(user);
		return "login";
	}

}
